package com.boot01.dao;

import com.boot01.pojo.Department;
import com.boot01.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

@Repository
public class EmployeeDao {

    private static final Map<Integer, Employee> employeeMap;

    @Autowired
    private DepartmentDao departmentDao;

    static {
        employeeMap = new HashMap<>();
        employeeMap.put(1001, new Employee(1001, "张晓红", "123@qq.com", 0, new Department(101, "市场部")));
        employeeMap.put(1002, new Employee(1002, "李小侠", "456@qq.com", 1, new Department(102, "运营部")));
        employeeMap.put(1003, new Employee(1003, "周晓琳", "789@qq.com", 0, new Department(103, "后勤部")));
        employeeMap.put(1004, new Employee(1004, "王小龙", "abc@qq.com", 1, new Department(104, "财务部")));
        employeeMap.put(1005, new Employee(1005, "方小丽", "efg@qq.com", 0, new Department(105, "人事部")));
    }

    public Collection<Employee> getEmployees() {
        return employeeMap.values();
    }

    public Employee getEmployeeById(Integer id) {
        return employeeMap.get(id);
    }

    public void deleteEmployee(Integer id) {
        employeeMap.remove(id);
    }

    private static Integer initId = 1006;

    public void addEmployee(Employee employee) {
        // 根据部门id，为客户添加部门信息
        employee.setDepartment(departmentDao.getDepartmentById(employee.getDepartment().getId()));

        if (employee.getId() == null) {
            // 如果id为空则新增id
            employee.setId(initId++);
            employeeMap.put(initId, employee);
        }else{
            // 如果不为空，则修改数据
            employeeMap.replace(employee.getId(),employee);
        }
    }
}
